<?php
##############################################
#
#   活动管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\ActivityModel;
use Admin\Model\DiscussModel;
use Admin\Model\OrderModel;


class ActivityController extends AdminBaseController {
	
	protected $_activity;
	
	public function _initialize(){
		parent::_initialize();
		$this -> _activity  = new ActivityModel();
	}
	
	/**
	 * 活动列表
	 */
	public function activityList(){
	    $data = $this->_activity->getActivity(I('activity_name'), I('type'), I('category'), I('nickname'), I('sponsor'), I('trial_status'));
	    foreach ($data[0] as $k => $v){
	            $data[0][$k]['category'] = $this->_activity->getCategoryName($v['category']);
	    }
	    $category = $this->_activity->getCategoryField();
	    $this->assign('category', $category);
	    $this -> assign('activity', $data[0]);
	    $this -> assign('page', $data[1]);
	    $this -> display('index');
	}
	
	/**
	 * 添加活动
	 */
 	public function addActivity(){
		if(IS_GET){
		    $id = I('id');
		    $act = I('get.act');
		    if($id && $act == 'edit'){
		        $data = $this->_activity->getActivityFind($id);
		        $data['describe'] = htmlspecialchars_decode($data['describe']);
		        $data['reason'] = htmlspecialchars_decode($data['reason']);
		        $data['start_time'] = date('Y-m-d H:i:s',$data['start_time']);
		        $data['end_time'] = date('Y-m-d H:i:s',$data['end_time']);
		        $this->assign('id', $id);
		        $this->assign('activity', $data);
		    }
		    $category = $this->_activity->getCategoryField();
		    $user = $this->_activity->getUser();
		    $this->assign('user',$user);
		    $this->assign('category', $category);
		    $this->assign('act', $act);
		    $this->display('add');
		}else if(IS_POST){
		    $data = I('post.');
		    if($_FILES['activity_img']['name']){
		        $photo_img = AdminBaseModel::upload(900, 255);
		        $data['activity_img'] = $photo_img['activity_img'];  //serialize($photo_img)
		        if($data['old_activity_img']){
		            @unlink($_SERVER['DOCUMENT_ROOT'].substr(__APP__, 1).$data['old_activity_img']);
		        }
		    }else{
		       if(!$data['old_activity_img']){
		          $this->error('请上传活动海报', '', 1);
		       }
		    }
		    if($data['trial_status'] == 3){
		        $data['reason'] = htmlspecialchars($data['reason']);
		    }else{
		        $data['reason'] = '';
		    }
		    //发送给用户消息
		    self::sendMessage($data['id'], $data['user_id'], $data['trial_status'], $data['activity_name'], $data['reason']);
		    $user_data = explode(',', $data['user']);
		    $data['user_id'] = $user_data[0];
		    $data['nickname'] = $user_data[1];
		    $data['start_time'] = strtotime($data['start_time']);
		    $data['end_time'] = strtotime($data['end_time']);
		    //$data['user_id'] = '';
		    $data['add_time'] = time();
		   	$admin_base_model = new AdminBaseModel('activity');
		   	$act = I('post.act');
		   	if($act == 'add'){
		   	    $result = $admin_base_model->addData($data);
		   	    if($result)
		   	        $this->success('添加成功', U('activityList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}elseif($act == 'edit'){
		   	    $id = I('id');
		   	    if($this->_activity->editActivity($id, $data) !== false)
		   	        $this -> success('修改成功', U('activityList'), 1);
		   	    else
		   	        $this -> error('操作失败', '', 1);
		   	}
    	}
	}
	
	/**
	 * 全选删除活动
	 */
    public function deleteAllActivity(){
	    if(!IS_POST) return;
	    $result = $this->_activity->deleteAllActivity($_POST);
	    if($result)
			$this -> success('删除成功', U('activityList'), 2);
		else
			$this -> error('操作失败', '', 2);
	}
	
	/**
	 * 删除活动
	 */
	public function deleteActivity(){
		if(!IS_AJAX) return;
		$count_data = M('activity_order')->where("activity_id = {$_GET['id']}")->count();
		if(!$count_data){
    		$result = $this->_activity->deleteActivity(intval($_GET['id']));
    		if($result)
    			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
    		else
    			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
		}else{
		    $this->ajaxReturn(array('status' => 3,'message' => '该活动下面有订单，不可删除！'));
		}
	}
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.activity_id');
	    if(!in_array($operate,array('trial_status','not_trial_status','is_top','hide','is_show_index')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $activity = M('activity');
	    switch ($operate){
	        case 'del':
	            $row = $activity->where($map)->delete();
	            break;
	        case 'trial_status':
	            $data = array('trial_status'=>2);
	            $row = $activity->where($map)->save($data);
	            break;
	        case 'not_trial_status':
	            $data = array('trial_status'=>3);
	            $row = $activity->where($map)->save($data);
	            break;
	        case 'is_top':
	            $data = array('is_top'=>1);
	            $row = $activity->where($map)->save($data);
	        case 'is_show_index':
	            $data = array('is_show_index'=>1);
	            $row = $activity->where($map)->save($data);
	    }
	    if(!$row)
	        $this->error('操作失败');
	    else
	        $this->success('操作成功', U('activityList'), 1);
	}
	
	/**
	 * 操作
	 */
	public function operate_signup(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.mobile');
	    if(!in_array($operate,array('send_sms')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    showData($selected_id_arr);
	    exit();
	    $map['id'] = array('in', $selected_id_arr);
	    $activity_order = M('activity_order');
	    switch ($operate){
	        case 'selected':
	            $data = array('is_selected'=>2);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'not_selected':
	            $data = array('is_selected'=>1);
	            $row = $activity_order->where($map)->save($data);
	            break;
	    }
	    if(!$row)
	        $this->error('操作失败');
	    else
	        $this->success('操作成功', U('signupList',array('activity_id'=>$activity_id)), 1);
	}
	
	/**
	 * 分类列表
	 */
	public function categoryList() {
	    $data = $this->_activity->getCategory(I('name'));
	    $this->assign('category', $data[0]);
	    $this->assign('page', $data[1]);
	    $this->display('category_index');
	}

	/**
	 * 添加分类
	 */
	public function addCategory() {
	    if(IS_GET){
	        $id = I('id');
	        $act = I('get.act');
	        if($id && $act == 'edit'){
	            $data = $this->_activity->getCategoryFind($id);
	            $data['describe'] = htmlspecialchars_decode($data['describe']);
	            $this->assign('id', $id);
	            $this->assign('category', $data);
	        }
	        $this->assign('act', $act);
	        $this->display('add_category');
	    }else if(IS_POST){
	        $data = I('post.');
	        $data['add_time'] = time();
	        $admin_base_model = new AdminBaseModel('activity_category');
	        $act = I('post.act');
	        if($act == 'add'){
	            $result = $admin_base_model->addData($data);
	            if($result)
	                $this->success('添加成功', U('categoryList'), 2);
	            else
	                $this->error('操作失败', '', 2);
	        }elseif($act == 'edit'){
	            $id = I('id');
	            if($this->_activity->editCategory($id, $data) !== false)
	                $this->success('修改成功', U('categoryList'), 2);
	            else
	                $this->error('操作失败', '', 2);
	        }
	    }
	}
	
	/**
	 * 删除分类
	 */
	public function deleteCategory() {
	    if(!IS_AJAX) return;
	    $count_data = M('activity')->where("category = {$_GET['id']}")->count();
	    if(!$count_data){
    		$result = $this->_activity->deleteCategory(intval($_GET['id']));
    		if($result)
    			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
    		else
    			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	    }else{
	        $this->ajaxReturn(array('status' => 3,'message' => '该分类下有活动，不可删除！'));
        }
	}
	
	/**
	 * 全选删除分类
	 */
	public function deleteAllCategory(){
	    if(!IS_POST) return;
	    $result = $this->_activity->deleteAllCategory($_POST);
	    if($result)
	        $this->success('删除成功', U('categoryList'), 2);
	    else
	        $this->error('操作失败', '', 2);
	}
	
	/**
	 * 操作
	 */
	public function operate_category(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.category_id');
	    if(!in_array($operate,array('del','show','hide')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $activity_category = M('activity_category');
	    switch ($operate){
	        case 'del':
	            $row = $activity_category->where($map)->delete();
	            break;
	        case 'show':
	            $data = array('is_show'=>1);
	            $row = $activity_category->where($map)->save($data);
	            break;
	        case 'hide':
	            $data = array('is_show'=>2);
	            $row = $activity_category->where($map)->save($data);
	            break;
	    }
	    if($row !== false)
	        $this->success('操作成功', U('categoryList'), 1);
	    else
	        $this->error('操作失败');
	     
	}
	
	/**
	 * 活动讨论
	 */
	public function Discuss(){
	    $id = I('id');
	    $discuss = new DiscussModel();
	    $data = $discuss->getDiscuss($id);
	    $this -> assign('discuss', $data[0]);
	    $this -> assign('page', $data[1]);
	    if ($_GET['is_ajax']) {
	        $this->display('ajax_discuss_list');
	        exit;
	    } 
	    $this->display('discuss');
	}
	
	/**
	 * 已报名
	 */
	public function signupList() {
	    $activity_id = I('activity_id');
	    $order_model = new OrderModel();
	    $data = $order_model->getSignup($activity_id, I('order_sn'), I('name'), I('mobile'), I('is_selected'));
	    $this->assign('activity_id', $activity_id);
	    $this->assign('signup', $data[0]);
	    $this->assign('page', $data[1]);
	    $this->display('signup_list');
	}
	
	/**
	 * 发送活动消息
	 */
	public function sendMessage($activity_id, $user_id, $trial_status, $activity_name, $reason){
	    
	    switch ($trial_status) {
	        case 2:
	        $data = array('activity_id'=>$activity_id, 'user_id'=>$user_id,
	                      'title'=>'活动审核通过', 
	                      'content'=>'恭喜你，你发布的 "'.$activity_name.'" 活动已经审核通过',
	                      'auth'=>'admin', 'add_time'=>time());
	        M('activity_message')->add($data);
	        break;
	        case 3:
	        $data = array('activity_id'=>$activity_id, 'user_id'=>$user_id,
	                      'title'=>'活动审核不予通过', 
	                      'content'=>'抱歉，你发布的  "'.$activity_name.'" 活动已经审核不予通过。<br/><p style="color:red;">原因：'.$reason.'</p>',
	                      'auth'=>'admin', 'add_time'=>time());
	        M('activity_message')->add($data);;
	        break;
	    }
	    return true;
	}
}



	
	